from flask_restful import Resource
from flask_restful.reqparse import RequestParser

from comment.modules.product import Product
from comment.modules.product_rate import Product_rate
from financial.resources.product.serializer import ProductRateListSerializer,ProductListSerializer


class InvestProduct(Resource):
    """
    理财产品的资源类
    """

    def get(self):
        """返回所有的理财产品列表"""
        product_list = Product.query.all()

        data = ProductListSerializer(product_list).to_dict()
        return {'msg': 'success', 'data': data}


class ProductRate(Resource):
    """
    产品利率资源类
    """

    def get(self):
        """返回一个产品的利率列表"""
        rp = RequestParser()
        rp.add_argument('proId', required=True)

        args = rp.parse_args()
        pid = args.proId
        rate_list = Product_rate.query.filter(Product_rate.productId == pid).all()

        data = ProductRateListSerializer(rate_list).to_dict()
        return {'msg': 'success', 'data': data}
